package cn.gailvlun.gll.presentation.emotion.util;

import android.databinding.BindingAdapter;
import android.databinding.ObservableField;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.ScreenUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTarget;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.presentation.emotion.core.BaseViewModel;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.LogUtil;
import cn.gailvlun.gll.util.SPUtil;



public class EmotionBindingAdapter {

    @BindingAdapter("loadCover")
    public static void loadCover(ImageView iv, ObservableField<String> url) {
        if (iv != null) {
            String cachePath = SPUtil.get(SPUtil.USER, BaseViewModel.Companion.getPREF_COVER_URI(), String.class);
            if (!IsEmpty.string(cachePath)) {
                Glide.with(iv.getContext())
                        .load(cachePath)
                        .into(iv);
            } else {
                if (!IsEmpty.string(url.get())) {
                    Glide.with(iv.getContext())
                            .load(url.get())
                            .into(iv);
                } else {
                    Glide.with(iv.getContext())
                            .load(url.get())


                            .load(R.drawable.bg_emotion_cover2)
                            .into(iv);
                }
            }
        }
    }

    @BindingAdapter("loadHeader")
    public static void loadHeader(ImageView iv, Object url) {
        if (iv != null) {
            RequestOptions requestOptions = RequestOptions
                    .placeholderOf(R.drawable.icon_emotion_header_placeholder_middle)
                    .error(R.drawable.icon_emotion_header_placeholder_middle)
                    .circleCrop();
            Glide.with(iv.getContext())
                    .load(url)
                    .apply(requestOptions)
                    .into(iv);
        }
    }

    @BindingAdapter("loadRounded")
    public static void loadRounded(ImageView iv, Object url) {
        if (iv != null) {
            Glide.with(iv.getContext())
                    .load(url)
                    .into(iv);
        }
    }

    @BindingAdapter(value = {"imageUrl", "imageWidth", "imageHeight", "halfScale"})
    public static void loadPhoto(final ImageView iv, Object url, final Integer imageWidth, final Integer imageHeight, final boolean halfScale) {
        if (iv != null) {

            if (imageHeight==0 || imageWidth==0) return;
            iv.requestLayout();
            ViewGroup.LayoutParams layoutParams = iv.getLayoutParams();

            LogUtil.d("hawks_loadPhoto", "scale:" + (imageHeight / imageWidth) + "" +
                    "-imageHeight:" + imageHeight + "-imageWidth:" + imageWidth);
            if (imageHeight > imageWidth) {
                if (halfScale) {
                    layoutParams.height = (imageHeight / imageWidth) * (ScreenUtils.getScreenWidth() / 2);

                } else {
                    layoutParams.height = (imageHeight / imageWidth) * (ScreenUtils.getScreenWidth());
                }
            } else {
                if (halfScale) {
                    layoutParams.height = (ScreenUtils.getScreenWidth() / 2) / (imageWidth / imageHeight);

                } else {
                    layoutParams.height= imageHeight;
                }
            }

            RequestOptions options = RequestOptions
                    .placeholderOf(R.drawable.bg_emotion_load_image_placeholder_middle)
                    .centerCrop();
            Glide.with(iv.getContext())
                    .load(url)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into(new ImageViewTarget<Drawable>(iv) {
                        @Override
                        protected void setResource(@Nullable Drawable resource) {
                            if (resource != null) {
                                iv.setImageDrawable(resource);
                            }
                        }
                    });
        }
    }

    @BindingAdapter(value = {"fullSizeUrl"})
    public static void loadFullSize(ImageView iv, Object url, Object thumbnailUrl) {
        Glide.with(iv.getContext())
                .load(url)
                .thumbnail(Glide.with(iv.getContext()).load(thumbnailUrl))
                .into(iv);
    }

    @BindingAdapter(value = {"thumbnailUrl"})
    public static void loadThumbnail(ImageView iv, Object url) {
        Glide.with(iv.getContext())
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(iv);
    }
}